package thread.threadpool;



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author : James
 * @Create : 2022-03-26-16:01
 * @Description :  SimpleDateFormat 定义成static全局常量，
 *               多线程环境下线程不安全
 */
public class TestSimpleDateFormat {


    // 1、创建单例实例

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String formatDate(Date date)  {
        return sdf.format(date);
    }

    public static Date parse(String strDate) throws java.text.ParseException {
        return sdf.parse(strDate);
    }

    public static void main(String[] args) throws InterruptedException {
        // 定义线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,
                4,
                10,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(20),new ThreadPoolExecutor.DiscardOldestPolicy());

        for (int i = 0; i < 20; i++) {
            threadPoolExecutor.execute(()->{

            });
        }

        threadPoolExecutor.shutdown();
        threadPoolExecutor.awaitTermination(1,TimeUnit.DAYS);

    }

}
